package com.abc.robot01.activity.compose

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.GenericShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.runtime.staticCompositionLocalOf
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import androidx.wear.compose.material.Icon
import androidx.wear.compose.material.LocalContentAlpha
import com.abc.robot01.R
import com.abc.robot01.activity.px

class CompositionLocalProviderDemo : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            val scrollState = rememberScrollState()
            Column(
                modifier = Modifier
                    .verticalScroll(scrollState)
                    .fillMaxSize()
            ) {
                SlotApiExample2()
                Spacer(modifier = Modifier.height(2.dp))
//                ThemedScreen()
                Spacer(modifier = Modifier.height(2.dp))
//                SlotApiExample()
                Spacer(modifier = Modifier.height(2.dp))
                Spacer(modifier = Modifier.height(2.dp))
                Spacer(modifier = Modifier.height(2.dp))
                Spacer(modifier = Modifier.height(2.dp))
            }
        }
    }
}

// 定义一个 CompositionLocal 用于共享颜色
val LocalContentColor = compositionLocalOf { Color.Blue}

@Composable
fun ThemedButton() {
    // 获取共享的颜色
    val buttonColor = LocalContentColor.current
    Button(
        onClick = { /* Do something */ },
        colors = ButtonDefaults.buttonColors(containerColor = buttonColor),
        modifier = Modifier.padding(16.dp)
    ) {
        Text("Themed Button")
    }
}

@Composable
fun ThemedScreen() {
    Column {
        // 提供一个新的颜色值
        CompositionLocalProvider(LocalContentColor provides Color.Green) {
            ThemedButton() // 这个按钮将使用绿色
        }
        // 提供一个新的颜色值
        CompositionLocalProvider(LocalContentColor provides Color.Red) {
            ThemedButton() // 这个按钮将使用red
        }
    }
}

@Composable
fun LocalContentColor01() {
    Column {
        CompositionLocalProvider(LocalContentColor provides Color.Red) {
            Button(onClick = { /*TODO*/ }) {
                Text(text = "确认", color = LocalContentColor.current)
            }
        }
    }
}

@Composable
fun LocalContentColor02() {
    CompositionLocalProvider(LocalContentAlpha provides 1.0f) {
        Icon(imageVector = Icons.Filled.Favorite, contentDescription ="" )
    }
}

@Composable
fun LocalContentColor03() {
    CompositionLocalProvider(LocalContentAlpha provides 0.5f) {
        Icon(imageVector = Icons.Filled.Favorite, contentDescription ="" )
    }
}

@Composable
fun LocalContentColor04() {
    CompositionLocalProvider(LocalContentAlpha provides 0.0f) {
        Icon(imageVector = Icons.Filled.Favorite, contentDescription ="" )
    }
}

@Composable
fun LocalContentColor05() {
    Icon(imageVector = Icons.Filled.Favorite, tint = Color.Blue ,contentDescription ="")
}



// 定义一个 CompositionLocal 用于共享主题颜色
//val LocalCustomColor = compositionLocalOf { Color.Red }



//@Composable
//fun CustomColorDemo() {
//    // 在 CompositionLocalProvider 中提供一个新的颜色值
//    CompositionLocalProvider(LocalCustomColor provides Color.Blue) {
//            CustomColorButton() // 按钮将使用提供的绿色颜色
//    }
//}


@Composable
fun CustomCard(
    title: @Composable () -> Unit,
    content: @Composable () -> Unit,
    actions: @Composable () -> Unit
) {
    Card(modifier = Modifier.padding(16.dp)) {
        Column(modifier = Modifier.padding(16.dp)) {
            title()
            Spacer(modifier = Modifier.height(8.dp))
            content()
            Spacer(modifier = Modifier.height(8.dp))
            actions()
        }
    }
}

@Composable
fun CustomCard2(content: @Composable BoxScope.() -> Unit,
) {
    Box(modifier = Modifier.padding(16.dp).fillMaxWidth()) {
        content()
    }
}

@Composable
fun SlotApiExample2() {
    CustomCard2(
        content = { Text(text = "This is the content of the card.",modifier = Modifier.align(Alignment.BottomCenter)) },
    )
}

@Composable
fun SlotApiExample() {
    CustomCard(
        title = { Text(text = "Title", style = MaterialTheme.typography.headlineMedium) },
        content = { Text(text = "This is the content of the card.") },
        actions = {
            Button(onClick = {}) {
                Text(text = "Action")
            }
        }
    )
}






